package com.liucxu.zimuzu.View.Fragement;

import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.liucxu.zimuzu.Base.BaseFragment;
import com.liucxu.zimuzu.Model.Bean.ArticalBean;
import com.liucxu.zimuzu.Model.Bean.ArticalEntity;
import com.liucxu.zimuzu.Presenter.IArticalPresenter;
import com.liucxu.zimuzu.Presenter.Impl.ArticalPresenterImpl;
import com.liucxu.zimuzu.R;
import com.liucxu.zimuzu.View.Adapter.ArticalAdapter;
import com.liucxu.zimuzu.View.IView.IArticalView;

import java.util.ArrayList;

/**
 * 资讯列表Fragment
 * Created by liukanshan on 2016/12/30.
 */

public class ArticalFragment extends BaseFragment implements IArticalView {
    RelativeLayout no_data;
    ImageView no_data_iv;
    SwipeRefreshLayout swipe_refresh;
    ListView content_list;
    private IArticalPresenter presenter;
    private boolean isRefresh = true;
    private int page = 1;
    private ArrayList<ArticalEntity> datas = new ArrayList<>();
    private ArticalAdapter adapter;
    private boolean stopLoadMore;
    private boolean isLoading;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_hot;
    }

    @Override
    public void setupView() {
        no_data = $(R.id.no_data);
        no_data_iv = $(R.id.no_data_iv);
        swipe_refresh = $(R.id.swipe_refresh);
        swipe_refresh.setColorSchemeColors(Color.parseColor("#0f88eb"));
        content_list = $(R.id.content_list);
        no_data_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                no_data.setVisibility(View.VISIBLE);
                swipe_refresh.setVisibility(View.GONE);
                getPageArticals(isRefresh);
            }
        });
        presenter = new ArticalPresenterImpl(this);
        adapter = new ArticalAdapter(getActivity(), datas);
        content_list.setAdapter(adapter);
        swipe_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                getPageArticals(isRefresh);
            }
        });
        content_list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int i) {

            }

            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                int last_visiable = absListView.getLastVisiblePosition();
                if (!stopLoadMore && !isLoading && (last_visiable > 0)) {
                    if (last_visiable + 2 > totalItemCount) {
                        isRefresh = false;
                        getPageArticals(isRefresh);
                    }
                }
            }
        });
    }

    @Override
    public void setupData() {
        isRefresh = true;
        getPageArticals(isRefresh);
    }

    @Override
    public void setArticals(ArticalBean artical) {
        if (artical != null) {
            if (artical.getStatus() == 1) {
                isLoading = false;
                if (isRefresh) {
                    page = 1;
                    datas.clear();
                    isRefresh = false;
                }
                page = page + 1;
                datas.addAll(artical.getData());
                adapter.setDatas(datas);
                if (datas.size() > 0) {
                    stopLoadMore = false;
                    no_data.setVisibility(View.GONE);
                    swipe_refresh.setVisibility(View.VISIBLE);
                } else {
                    stopLoadMore = true;
                    no_data.setVisibility(View.VISIBLE);
                    swipe_refresh.setVisibility(View.GONE);
                }
            } else {
                isLoading = false;
                stopLoadMore = false;
                no_data.setVisibility(View.VISIBLE);
                swipe_refresh.setVisibility(View.GONE);
            }
        } else {
            isLoading = false;
            stopLoadMore = false;
            no_data.setVisibility(View.VISIBLE);
            swipe_refresh.setVisibility(View.GONE);
        }
    }

    @Override
    public void showLoading() {
        if (isRefresh) {
            swipe_refresh.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        swipe_refresh.setRefreshing(false);
    }

    @Override
    public void showError(String errMsg) {
        Snackbar.make(swipe_refresh, errMsg, Snackbar.LENGTH_SHORT).show();
    }

    private void getPageArticals(boolean refresh) {
        int current_page = 1;
        if (refresh)
            current_page = 1;
        else
            current_page = page;
        if (!refresh) {
            if (stopLoadMore) {
                return;
            } else {
                stopLoadMore = true;
                isLoading = true;
            }
        } else {
            isLoading = true;
            stopLoadMore = true;
        }

        presenter.getArticals(current_page);
    }
}
